package com.flycat.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * @Author yalei.chen
 * @Date 2017/9/3
 * @Since JDK 1.8
 */
@Configuration
public class DruidPoolConfig {


    private static final Logger log = LoggerFactory.getLogger(DruidPoolConfig.class);

    @Autowired
    private DruidConfigProperty druidConfigProperty;

    @Bean
    public DataSource druidDataSource(){
        DruidDataSource ds = new DruidDataSource();
        ds.setDriverClassName(druidConfigProperty.getDriverClassName());
        ds.setUrl(druidConfigProperty.getUrl());
        ds.setUsername(druidConfigProperty.getUsername());
        ds.setPassword(druidConfigProperty.getPassword());
        ds.setMaxActive(druidConfigProperty.getMaxActive());
        ds.setInitialSize(druidConfigProperty.getInitialSize());
        ds.setMaxWait(druidConfigProperty.getMaxWait());
        ds.setMinIdle(druidConfigProperty.getMinIdle());
        ds.setTimeBetweenEvictionRunsMillis(druidConfigProperty.getTimeBetweenEvictionRunsMillis());
        ds.setMinEvictableIdleTimeMillis(druidConfigProperty.getMinEvictableIdleTimeMillis());
        ds.setValidationQuery(druidConfigProperty.getValidationQuery());
        ds.setTestWhileIdle(druidConfigProperty.isTestWhileIdle());
        ds.setTestOnBorrow(druidConfigProperty.isTestOnBorrow());
        ds.setTestOnReturn(druidConfigProperty.isTestOnReturn());
        ds.setPoolPreparedStatements(druidConfigProperty.isPoolPreparedStatements());
        ds.setMaxOpenPreparedStatements(druidConfigProperty.getMaxOpenPreparedStatements());
        try {
            ds.setFilters(druidConfigProperty.getFilters());
        } catch (SQLException e) {
            e.printStackTrace();
            log.error("druid err:{}", e);
        }
        return ds;
    }

    @Bean
    public ServletRegistrationBean druidServlet(){
        ServletRegistrationBean reg = new ServletRegistrationBean();
        reg.setServlet(new StatViewServlet());
        reg.addUrlMappings("/druid/*");
        //reg.addInitParameter("allow", "127.0.0.1");
        //reg.addInitParameter("deny","");
        reg.addInitParameter("loginUsername", druidConfigProperty.getLoginUserName());
        reg.addInitParameter("loginPassword", druidConfigProperty.getLoginPassword());
        reg.addInitParameter("resetEnable","false");//是否能购重置数据
        return reg;
    }

    @Bean
    public FilterRegistrationBean filterRegistrationBean(){
        FilterRegistrationBean fb = new FilterRegistrationBean();
        fb.setFilter(new WebStatFilter());
        fb.addUrlPatterns("/*");
        fb.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return fb;
    }
}
